/*
 * Copyright (C) 2012 Artem Bakulin
 *
 * This file is part of Speech Fluency for Android
 * (http://code.google.com/p/speech-fluency-droid/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.google.code.speechfluency;

import java.io.Serializable;

import android.content.Intent;
import android.content.SharedPreferences;

/**
 * User-defined settings for delayed audio feedback
 * 
 * @author Artem Bakulin
 */
public class Settings implements Serializable {

    private static final long serialVersionUID = 5346256371148516622L;

    private static final String INTENT_FIELD = "settings";
    
    private static final String DELAY_KEY = "delay";
    private static final String BOOST_KEY = "boost";
    private static final String ACTIVE_KEY = "active";
    private static final String MUTE_WITHOUT_HEADSET_KEY = "mute_without_headset";
    
    private static final float DEFAULT_DELAY_IN_SECONDS = 0.2f;
    private static final float DEFAULT_BOOST_IN_DB = 2.0f;
    private static final boolean ACTIVE_BY_DEFAULT = false;
    private static final boolean MUTE_WITHOUT_HEADSET_BY_DEFAULT = false;
    
    private float delayInSeconds;
    private float boostInDb;
    private boolean active;
    private boolean muteWithoutHeadset;
    
    public static Settings extractFromIntent(Intent intent) {
        return (Settings)intent.getSerializableExtra(INTENT_FIELD);
    }
    
    public void addToIntent(Intent intent) {
        intent.putExtra(INTENT_FIELD, this);
    }
    
    public void dumpToPreferences(SharedPreferences preferences) {
        preferences
            .edit()
            .clear()
            .putFloat(DELAY_KEY, delayInSeconds)
            .putFloat(BOOST_KEY, boostInDb)
            .putBoolean(ACTIVE_KEY, active)
            .putBoolean(MUTE_WITHOUT_HEADSET_KEY, muteWithoutHeadset)
            .commit();
    }
    
    public static Settings restoreFromPreferences(SharedPreferences preferences) {
        Settings s = new Settings();
        s.delayInSeconds = preferences.getFloat(DELAY_KEY, DEFAULT_DELAY_IN_SECONDS);
        s.boostInDb = preferences.getFloat(BOOST_KEY, DEFAULT_BOOST_IN_DB);
        s.active = preferences.getBoolean(ACTIVE_KEY, ACTIVE_BY_DEFAULT);
        s.muteWithoutHeadset = preferences.getBoolean(MUTE_WITHOUT_HEADSET_KEY, MUTE_WITHOUT_HEADSET_BY_DEFAULT);
        return s;
    }
    
    public float getDelayInSeconds() {
        return delayInSeconds;
    }
    
    public void setDelayInSeconds(float delayInSeconds) {
        this.delayInSeconds = delayInSeconds;
    }
    
    public float getBoostInDb() {
        return boostInDb;
    }
    
    public void setBoostInDb(float boostInDb) {
        this.boostInDb = boostInDb;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
    
    public boolean isMuteWithoutHeadset() {
        return muteWithoutHeadset;
    }

    public void setMuteWithoutHeadset(boolean muteWithoutHeadset) {
        this.muteWithoutHeadset = muteWithoutHeadset;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (active ? 1231 : 1237);
        result = prime * result + (muteWithoutHeadset ? 1231 : 1237);
        result = prime * result + Float.floatToIntBits(boostInDb);
        result = prime * result + Float.floatToIntBits(delayInSeconds);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Settings)) {
            return false;
        }
        
        Settings other = (Settings) obj;
        return this.active == other.active
            && this.muteWithoutHeadset == other.muteWithoutHeadset
            && areEqual(this.boostInDb, other.boostInDb)
            && areEqual(this.delayInSeconds, other.delayInSeconds);
    }
    
    private boolean areEqual(float a, float b) {
        return Float.floatToIntBits(a) == Float.floatToRawIntBits(b);
    }
}
